<?php

namespace app\api\controller;

use app\common\controller\Api;
use fast\Random;
use think\Config;
use think\Validate;
use think\Db;
use app\admin\model\Banner;
use app\admin\model\UserFeedback;


/**
 * 会员接口
 */
class Venue extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    /*
     * 场馆列表
     * */
    public function getVenue()
    {
        $venuelist = Db::name('venue')->where('status', 0)->field('id,name')->where('deletetime',null)->where('status',0)->select();
        $this->success('场馆列表', $venuelist);
    }

    /*
     * 场馆详情
     * */
    public function venueInfo()
    {
        $this->checkParam('id');
        $id = $this->request->request('id');
        $venueInfo = Db::name('venue')->where('id', $id)->field('id,name,address,latitude,longitude,address_image,images,stime,etime,ext')->find();
        if (!$venueInfo) {
            $this->error(__('暂无数据'));
        }
        $url = (isHTTPS() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'];
        if (!empty($venueInfo['images'])){
            $imageList = explode(',',$venueInfo['images']);
            $temp = [];
            foreach ($imageList as $item) {
                $temp[] .= $url. $item;
            }
            $venueInfo['images'] = $temp;
        }
        if (!empty($venueInfo['address_image'])){
            $venueInfo['address_image'] = $url. $venueInfo['address_image'];
        }
        $time = date('H:i:s',time());
        $strtime = strtotime($time);
        /*$venueInfo['s'] = strtotime($venueInfo['stime']);
        $venueInfo['e'] = strtotime($venueInfo['etime']);*/
        if ($strtime < strtotime($venueInfo['stime']) || $strtime > strtotime($venueInfo['etime'])) {
            $venueInfo['doBusiness'] = false;
        } else {
            $venueInfo['doBusiness'] = true;
        }
        $this->success('场馆详情', $venueInfo);
    }

    /*
     *
     * 获取场馆Banner
     * */
    public function getBanner()
    {
        $url = (isHTTPS() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'];
        $this->checkParam('id');
        $id = $this->request->request('id');
        $ret = Banner::where('venue_id', $id)->where('deletetime',null)->where('status',0)->select()->toArray();
        if ($ret) {
            foreach ($ret as &$v) {
                if ($v['image']) {
                    $v['image'] = $url. $v['image'];
                }
            }

        }
        $this->success(__('banner列表'), $ret);
    }

    /*
     *
     * 问题与反馈
     *
     * */
    public function feedback()
    {
        $ret = \app\common\model\User::where('id', $this->auth->id)->find();
        $data = $this->request->post();
        $data['time'] = time();
        $data['user_id'] = $this->auth->id;
        $data['venue_id'] = $ret['venue_id'];
        UserFeedback::insert($data);
        $this->success(__('已收到您的反馈，我们会尽快解决处理'));
    }

    /*
     *
     * 明星教练
     * */

    public function coachList()
    {
        $list = \app\common\model\User::where('coach', 1)->field('id,username,age,image,coach_score,label,profile,star')->select()->toArray();
        if ($list) {
            foreach ($list as &$v) {
                $v['image'] = cdnurl($v['image'], true);
                $v['course'] = DB::name('course')->where('find_in_set(' . $v['id'] . ',user_ids)')->where('deletetime',null)->where('status',0)->column('name');
            }
        }
        $this->success(__('明星教练'),$list);
    }


    /*
     *
     * 教练详情
     * */
    public function coachInfo()
    {
        $this->checkParam(['id','uid']);
        $id = $this->request->request('id');
        $list = \app\common\model\User::where('star', 1)->where('id', $id)->field('id,username,age,image,coach_score,label,profile,star')->find()->toArray();
        $uid = $this->request->request('uid');
        $score = Db::table('keep_user_course')
            ->where('coach_id',$id)
            ->where('user_id',$uid)
            ->where('type','>', 1)
            ->where('status', 0)
            ->find();
        if ($score && empty($score['coach_score'])) {
            $list['user_score'] = true;
        } else {
            $list['user_score'] = false;
        }
        $list['image'] = cdnurl($list['image'], true);
        if ($list) {
            $list['course'] = Db::table('keep_term')
                ->alias('t')
                ->join('keep_course c','c.id = t.course_id')
                ->join('keep_venue v','v.id = t.venue_id')
                ->where('t.user_id',$id)
                ->order('t.stime')
                ->field('t.stime, v.name as venueName,c.id as course_id, c.name as courseName, c.price, c.day,c.image,c.type, t.num')
                ->select()
                ->toArray();
            foreach ($list['course'] as &$v) {
                $v['image'] = cdnurl($v['image'], true);
            }
        }
        $this->success(__('教练详情'),$list);
    }

    /*
     *
     * 明星学员
     * */

    public function starList()
    {
        $list = \app\common\model\User::where('star', 1)->where('coach', 0)->field('id,username,age,gender,school,image')->order('weigh')->select()->toArray();
        if ($list) {
            foreach ($list as &$v) {
                $v['course'] = Db::table('keep_user_course')
                    ->alias('u')
                    ->join('keep_course c','c.id = u.course_id')
                    ->where('u.user_id',$v['id'])
                    ->field(' c.name as courseName')
                    ->find();
                $v['image'] = cdnurl($v['image'], true);
            }
        }
        $this->success(__('明星学员列表'),$list);
    }

    /*
     *
     * 明星学员详情
     * */
    public function starInfo()
    {
        $id = $this->request->request('id');
        $info = \app\common\model\User::where('star', 1)->where('id', $id)->field('id,username,age,gender,school,image,school,profile')->find();
        $info['image'] = cdnurl($info['image'], true);
        $info['course'] = Db::table('keep_user_course')
            ->alias('u')
            ->join('keep_course c','c.id = u.course_id')
            ->where('u.user_id',$id)
            ->field(' c.name as courseName')
            ->find();

        $this->success(__('明星学员'),$info);
    }

    /*
     * 给教练评分
     * */
    public function coachScore() {
        $this->checkParam(['id', 'score', 'uid']);
        $uid = $uid = $this->request->request('uid');
        $id = $this->request->request('id');
        $score = $this->request->request('score');
        $coachScore = Db::table('keep_user_course')
            ->where('coach_id',$id)
            ->where('user_id',$uid)
            ->where('type','>', 1)
            ->where('status', 0)
            ->find();
        if (!$coachScore) {
            $this->error('您未上过该教练的课，无法评分！');
        }
        if ($coachScore && $coachScore['coach_score']) {
            $this->error('您已评分！');
        }
        Db::table('keep_user_course')
            ->where('id',$coachScore['id'])->update(['coach_score' => $score]);

        $totall = Db::table('keep_user_course')
            ->where('coach_id',$id)
            ->where('status', 0)
            ->sum('coach_score');

        if ($totall > 0) {
            $count =Db::table('keep_user_course')
                ->where('coach_id',$id)
                ->where('status', 0)
                ->count();
            $coach_score = $totall/$count;
            Db::table('keep_user')
                ->where('id',$id)->update(['coach_score' => $coach_score]);
        }

        $this->success(__('评分成功'));
    }

    public function getAgreement($id)
    {
        $ret = Db::name('agreement')->where('id', $id)->field('id,name,content')->find();
        $this->success(__($ret['name']), $ret);
    }
}
